package com.dengqi.mymodule.activity.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.dengqi.mymodule.BaseActivity;
import com.dengqi.mymodule.R;
import com.module.dialog.CustomDialog;
import com.module.dialog.CustomListDialog;
import com.module.dialog.EditableDialog;
import com.module.dialog.ImageDialog;
import com.module.dialog.ListDialog;
import com.module.dialog.MessageDialog;
import com.module.dialog.PickerDialog;
import com.module.dialog.ProgressDialog;
import com.module.dialog.adapter.CustomAdapter;
import com.module.dialog.bean.Data;
import com.module.dialog.inter.DialogOnClickListener;
import com.module.dialog.inter.DialogOnItemListener;
import com.module.dialog.utils.ClickType;
import com.module.dialog.utils.DialogStyle;
import com.module.dialog.utils.ProgressType;
import com.module.retrofit.utils.ToastUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class DialogActivity extends BaseActivity {

    private List<TestBean> data;
    private List<String> options1Items = new ArrayList<>();
    private List<List<String>> options2Items = new ArrayList<>();
    private List<List<List<String>>> options3Items = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        data = new ArrayList<>();
        data.add(new TestBean("1"));
        data.add(new TestBean("2"));
        data.add(new TestBean("3"));
        data.add(new TestBean("4"));
    }

    public void onBtn1(View view) {
        new AlertDialog.Builder(this).setTitle("title").setMessage("message").setPositiveButton("sure", null)
                .create()
                .show();
    }

    public void onBtn2(View view) {
//        RxDialog.with(this)
//                .messageDialog("ssss")
//                .suber();
        new MessageDialog(this)
                .setMessage("message")
                .setTitle("title")
                .setButton1("sure")
                .setButton2("cancel")
                .setDialogOnClickListener(new DialogOnClickListener() {
                    @Override
                    public void onClick(ClickType which) {
                        if(which == ClickType.Sure){
                            ToastUtil.showToast("sure");
                        }else if(which == ClickType.Cancel){
                            ToastUtil.showToast("cancel");
                        }
                    }
                })
                .build()
                .show();



    }

    public void onBtn3(View view) {
        new ListDialog<TestBean>(this)
                .setItems(data, new DialogOnItemListener() {
                    @Override
                    public void onItemClick(int position) {
                        Toast.makeText(DialogActivity.this, position + " - " + data.get(position).getText(), Toast.LENGTH_SHORT).show();
                    }

                })
                .setTitle("list")
                .build()
                .show();
    }

    public void onBtn4(View view) {
        new ListDialog(this).setSingleChoiceItems(data).setCallBack(new ListDialog.DialogCallBack() {
            @Override
            public void callback(int which, int[] positions, String result) {
                Toast.makeText(DialogActivity.this, which + " - " + Arrays.toString(positions) + " - " + result, Toast.LENGTH_SHORT).show();
            }
        }).setTitle("radio").setButton1("确定").setButton2("取消").build().show();
    }

    public void onBtn5(View view) {
        new ListDialog(this).setMultiChoiceItems(data).setCallBack(new ListDialog.DialogCallBack() {
            @Override
            public void callback(int which, int[] positions, String result) {
                Toast.makeText(DialogActivity.this, which + " - " + Arrays.toString(positions) + " - " + result, Toast.LENGTH_SHORT).show();
            }
        }).setTitle("checkbox").setButton1("确定").build().show();
    }

    public void onBtn6(View view) {
        new EditableDialog(this).setCallBack(new EditableDialog.DialogCallBack() {
            @Override
            public void callback(int which, String result) {
                Toast.makeText(DialogActivity.this, which + " - " + result, Toast.LENGTH_SHORT).show();
            }
        }).setTitle("editable").setButton1("确定").build().show();
    }

    public void onBtn7(View view) {
        new ImageDialog(this).setImageRes(R.drawable.img).setTitle("image").setButton1("确定").build().show();
    }

    public void onBtn8(View view) {
        Dialog dialog = new ProgressDialog(this)
                .setStyle(ProgressType.LoadingHorizontal)
                .setTitle("progress")
                .setButton1("确定").build();
        dialog.show();
        ProgressBar progressBar = dialog.findViewById(R.id.progressBar2);
        progressBar.setMax(100);
        handler = new MyHandler(this);
        new Thread(new MyThread(progressBar)).start();

    }

    public void onBtn9(View view) {
        List<String> data = new ArrayList<>();
        data.add("拍照");
        data.add("相册");
        new ListDialog<String>(this)
                .setItems(data, new DialogOnItemListener() {
                    @Override
                    public void onItemClick(int position) {
                        Toast.makeText(DialogActivity.this, position + "", Toast.LENGTH_SHORT).show();
                    }
                })
                .setDialogStyle(DialogStyle.Bottom)
                .setButton1("取消")
                .build()
                .show();

    }

    public void onBtn10(View view) {
        initData();
        //条件选择器
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String string = options1Items.get(options1) +
                        options2Items.get(options1).get(options2) +
                        options3Items.get(options1).get(options2).get(options3);
                Toast.makeText(DialogActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        }).setTitleText("123").build();
        pvOptions.setPicker(options1Items, options2Items, options3Items);
        pvOptions.show();
    }

    public void onBtn11(View view) {
        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                Toast.makeText(DialogActivity.this, date.toString(), Toast.LENGTH_SHORT).show();
            }
        }).build();
        pvTime.show();
    }

    public void onBtn12(View view) {
        new PickerDialog<TestBean>(this)
                .setItems(data, new DialogOnItemListener() {
                    @Override
                    public void onItemClick(int position) {
                        Toast.makeText(DialogActivity.this, data.get(position).getPickerViewText(), Toast.LENGTH_SHORT).show();
                    }
                })
                .build().show();
    }

    public void onBtn13(View view) {
        new CustomDialog(this)
                .setContentView(new CustomDialog.IContentView() {
                    @Override
                    public int getLayoutRes() {
                        return R.layout.dialog_custom2;
                    }

                    @Override
                    public void bindDatas(View parent) {
//                        TextView textView = parent.findViewById(R.id.tv_test);
//                        textView.setText("123132123sad21133");
                    }
                })
                .setTitle("123")
                .setButton1("123")
                .build()
                .show();
    }

    public void onBtn14(View view) {
        List<TestBean> list = new ArrayList<>();
        list.add(new TestBean("123123"));
        list.add(new TestBean("123123"));
        list.add(new TestBean("123123"));
        list.add(new TestBean("123123"));
        list.add(new TestBean("123123"));
        list.add(new TestBean("123123"));
        new CustomListDialog<TestBean>(this)
                .setAdapter(new CustomAdapter<TestBean>(this) {
                    @Override
                    public int getLayoutRes() {
                        return R.layout.dialog_custom;
                    }

                    @Override
                    public void bindData(CustomViewHolder holder, int position, List<TestBean> datas) {
                        TextView textView = holder.itemView.findViewById(R.id.tv_test);
                        textView.setText(datas.get(position).getText());
                    }
                })
                .setItems(list)
                .setTitle("list")
                .setButton1("123")
                .setButton2("3434")
                .build()
                .show();

    }

    public void onBtn15(View view) {
        DialogUtil.deleteDialog(this, null);
    }

    public void onBtn16(View view) {

    }

    private static MyHandler handler;

    static class MyHandler extends Handler {
        WeakReference<DialogActivity> weakReference;

        public MyHandler(DialogActivity activity) {
            weakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(weakReference.get().getApplicationContext(), "完成", Toast.LENGTH_SHORT).show();
        }
    }

    static class MyThread implements Runnable {
        ProgressBar progressBar;

        public MyThread(ProgressBar progressBar) {
            this.progressBar = progressBar;
        }

        @Override
        public void run() {
            int max = 100;
            for (int i = 0; i <= max; i++) {
                progressBar.setProgress(i);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            handler.sendEmptyMessage(0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            //如果参数为null的话，会将所有的Callbacks和Messages全部清除掉。
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }

    private void initData() {
        //省份
        ArrayList<Data> list = new ArrayList<>();
        //添加3个个省份
        Data data1 = new Data();
        Data data2 = new Data();
        Data data3 = new Data();
        data1.setName("江西省");
        data2.setName("河南省");
        data3.setName("湖北省");
        //添加市数据（省下面的数据）
        ArrayList<Data.CityBean> cityBeans1 = new ArrayList<>();
        ArrayList<Data.CityBean> cityBeans2 = new ArrayList<>();
        ArrayList<Data.CityBean> cityBeans3 = new ArrayList<>();
        //（第一个省份下面的市数据）
        Data.CityBean cityBean1 = new Data.CityBean();
        Data.CityBean cityBean2 = new Data.CityBean();
        Data.CityBean cityBean3 = new Data.CityBean();
        cityBean1.setName("南昌");
        cityBean2.setName("九江");
        cityBean3.setName("赣州");
        cityBeans1.add(cityBean1);
        cityBeans1.add(cityBean2);
        cityBeans1.add(cityBean3);
        //（第二个省份下面的市数据）
        Data.CityBean cityBean4 = new Data.CityBean();
        Data.CityBean cityBean5 = new Data.CityBean();
        Data.CityBean cityBean6 = new Data.CityBean();
        cityBean4.setName("洛阳");
        cityBean5.setName("开封");
        cityBean6.setName("许昌");
        cityBeans2.add(cityBean4);
        cityBeans2.add(cityBean5);
        cityBeans2.add(cityBean6);
        //（第三个省份下面的市数据）
        Data.CityBean cityBean7 = new Data.CityBean();
        Data.CityBean cityBean8 = new Data.CityBean();
        Data.CityBean cityBean9 = new Data.CityBean();
        cityBean7.setName("黄冈");
        cityBean8.setName("荆州");
        cityBean9.setName("鄂州");
        cityBeans3.add(cityBean7);
        cityBeans3.add(cityBean8);
        cityBeans3.add(cityBean9);

        //为市添加区（一共为9个市）
        ArrayList<String> area1 = new ArrayList<>();
        ArrayList<String> area2 = new ArrayList<>();
        ArrayList<String> area3 = new ArrayList<>();
        ArrayList<String> area4 = new ArrayList<>();
        ArrayList<String> area5 = new ArrayList<>();
        ArrayList<String> area6 = new ArrayList<>();
        ArrayList<String> area7 = new ArrayList<>();
        ArrayList<String> area8 = new ArrayList<>();
        ArrayList<String> area9 = new ArrayList<>();
        area1.add("东湖区");
        area1.add("西湖区");
        area1.add("青云谱区");
        area2.add("浔阳区");
        area2.add("濂溪区");
        area2.add("九江县");
        area3.add("赣县");
        area3.add("信丰");
        area3.add("南康");
        area4.add("西工区");
        area4.add("洛龙区");
        area4.add("涧西区");
        area5.add("鼓楼区");
        area5.add("龙亭区");
        area5.add("金明区");
        area6.add("禹州市");
        area6.add("长葛市");
        area6.add("许昌县");
        area7.add("团风县");
        area7.add("浠水县");
        area7.add("罗田县");
        area8.add("荆州区");
        area8.add("沙市区");
        area8.add("江陵县");
        area9.add("鄂城区");
        area9.add("华容区");
        area9.add("梁子湖区");

        //将区添加至市中（27个区）
        cityBean1.setArea(area1);
        cityBean2.setArea(area2);
        cityBean3.setArea(area3);
        cityBean4.setArea(area4);
        cityBean5.setArea(area5);
        cityBean6.setArea(area6);
        cityBean7.setArea(area7);
        cityBean8.setArea(area8);
        cityBean9.setArea(area9);

        //将市添加至省中（9个市）
        data1.setCity(cityBeans1);
        data2.setCity(cityBeans2);
        data3.setCity(cityBeans3);

        //将市添加至省中(3个省)
        list.add(data1);
        list.add(data2);
        list.add(data3);

        for (int i = 0; i < list.size(); i++) {//遍历省份
            List<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            List<List<String>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）
            for (int c = 0; c < list.get(i).getCity().size(); c++) {//遍历该省份的所有城市
                String CityName = list.get(i).getCity().get(c).getName();
                CityList.add(CityName);//添加城市
                List<String> City_AreaList = new ArrayList<>();//该城市的所有地区列表
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (list.get(i).getCity().get(c).getArea() == null
                        || list.get(i).getCity().get(c).getArea().size() == 0) {
                    City_AreaList.add("");
                } else {
                    City_AreaList.addAll(list.get(i).getCity().get(c).getArea());
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }
            options1Items.add(list.get(i).getName());
            options2Items.add(CityList);
            options3Items.add(Province_AreaList);

        }

    }
}
